﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;


namespace MiaProject
{
    class Enemy2D : Actor2D
    {
        private const float Rotation = 0;
        private const float Scale = 1.0f;
        private const float Depth = 0.5f;
        
        public int frames;
        public int framesPerSec;
        public const int ENEMY_MOVE_RIGHT = 0;
        public const int ENEMY_MOVE_LEFT = 1;
        public const int ENEMY_STOPED = 2;
        public Vector2 pos;
        public Vector2 dir;
        public List<Vector2> points;
        public Explosion2D exp;
        private int currentPoint;
        public bool status;

        public Enemy2D()
        {
            SpriteTexture = new AnimatedTexture(Vector2.Zero, Rotation, Scale, Depth);
            this.frames = 8;
            this.framesPerSec = 2;
            this.animation = new int[3, 2];
            this.pos = Vector2.Zero;
            this.currentPoint = 0;

            this.animation[ENEMY_MOVE_LEFT, 0] = 0;
            this.animation[ENEMY_MOVE_LEFT, 1] = 3;

            this.animation[ENEMY_MOVE_RIGHT, 0] = 4;
            this.animation[ENEMY_MOVE_RIGHT, 1] = 7;

            this.animation[ENEMY_STOPED, 0] = 1;
            this.animation[ENEMY_STOPED, 1] = 2;

            
            this.dir = new Vector2(1,0);

            this.status = true;
            this.currentAnim = -1;
            
        }
        public bool animEnded()
        {
            return SpriteTexture.animEndend;
        }
        public void setAnimMoveLeft()
        {
            if (this.currentAnim == ENEMY_MOVE_LEFT)
                return;
            this.currentAnim = ENEMY_MOVE_LEFT;
            SpriteTexture.setAnim(getAnimI(), getAnimF());
        }
        public void setAnimMoveRight()
        {
            if (this.currentAnim == ENEMY_MOVE_RIGHT)
                return;
            this.currentAnim = ENEMY_MOVE_RIGHT;
            SpriteTexture.setAnim(getAnimI(), getAnimF());
        }
        public void setAnimMoveStoped()
        {
            if (this.currentAnim == ENEMY_STOPED)
                return;
            this.currentAnim = ENEMY_STOPED;
            SpriteTexture.setAnim(getAnimI(), getAnimF());
        }
        
        public void update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (this.status && this.points.Count > 0)
            {
                if (Vector2.Distance(this.pos, this.points[this.currentPoint]) <= 5)
                {
                    this.currentPoint++;
                    if (this.currentPoint >= this.points.Count)
                        this.currentPoint = 0;

                    dir = this.points[this.currentPoint] - pos;
                    dir.Normalize();

                    if (this.dir.X < 0)
                        this.setAnimMoveLeft();
                    else
                        this.setAnimMoveRight();
                     
                }

                this.pos.X += 20 * elapsed * this.dir.X;
            }
            this.SpriteTexture.UpdateFrame(elapsed);

            

            exp.explosion.Update(gameTime);
            exp.smoke.Update(gameTime);
        }
        /*public bool isSended()
        {
            if(SpriteTexture.Frame >= 23)
                return true;
            return false;
        }*/

        public void createExplosion(Vector2 where)
        {
            exp.explosion.AddParticles(where);
            exp.smoke.AddParticles(where);
        }

    }
}
